<template>
<view class="content">
	<view class="box">
		<view class="logo">
			<image src='http://codegen.caihongy.cn/20220212/ad27dde95af143db9a6447f869150241.jfif' mode="aspectFill"></image>
			<view class="title">用户注册</view>
		</view>
		<view class="form-item" v-if="tableName=='yonghu'">
			<input v-model="ruleForm.zhanghao" type="text" class="input" name="" placeholder="请输入账号" />
		</view>
		<view class="form-item" v-if="tableName=='yonghu'">
			<input v-model="ruleForm.mima" type="password" class="input" name="" placeholder="请输入密码" />
		</view>
		<view class="form-item" v-if="tableName=='yonghu'">
			<input v-model="ruleForm.mima2" type="password" class="input" name="" placeholder="请确认密码" />
		</view>
		<view class="form-item" v-if="tableName=='yonghu'">
			<input v-model="ruleForm.nicheng" type="text" class="input" name="" placeholder="请输入昵称" />
		</view>
		<view class="form-item" v-if="tableName=='yonghu'">
			<picker @change="yonghuxingbieChange" :value="yonghuxingbieIndex" :range="yonghuxingbieOptions">
				<view class="picker-view">{{ruleForm.xingbie?ruleForm.xingbie:"请选择性别"}}</view>
			</picker>
		</view>
		<view class="form-item" v-if="tableName=='yonghu'">
			<input v-model="ruleForm.shouji" type="text" class="input" name="" placeholder="请输入手机号" />
		</view>
		<view class="form-item" v-if="tableName=='yonghu'">
			<input v-model="ruleForm.youxiang" type="text" class="input" name="" placeholder="请输入邮箱" />
		</view>
		<button class="btn-submit" @tap="register" type="primary">注册</button>
		<view class="links">
			<view class="link-item" @tap="goLogin">已有账号？去登录</view>
		</view>
	</view>
</view>
</template>

<script>
	export default {
		data() {
			return {
                yonghuxingbieOptions: [],
                yonghuxingbieIndex: 0,
				ruleForm: {
				},
				emailcode: "",
				tableName:""
			}
		},
		async onLoad() {
			let res = [];
			let table = uni.getStorageSync("loginTable");
	    	this.tableName = table;

            // 自定义下拉框值
			if(this.tableName=='yonghu'){
                this.yonghuxingbieOptions = "男,女".split(',');
				this.ruleForm.xingbie=this.yonghuxingbieOptions[0]
			}
			
			this.styleChange()
		},
		methods: {
            // 下拉变化
            yonghuxingbieChange(e) {
                this.yonghuxingbieIndex = e.target.value
                this.ruleForm.xingbie = this.yonghuxingbieOptions[this.yonghuxingbieIndex]
            },

			styleChange() {
				this.$nextTick(()=>{
					// document.querySelectorAll('.uni-input .uni-input-input').forEach(el=>{
					//   el.style.backgroundColor = this.registerFrom.content.input.backgroundColor
					// })
				})
			},
			// 获取uuid
			getUUID () {
				return new Date().getTime();
			},
			// 返回登录页
			goLogin() {
				this.$utils.jump('../login/login');
			},
			// 注册
			async register() {
				if((!this.ruleForm.zhanghao) && `yonghu` == this.tableName){
					this.$utils.msg(`账号不能为空`);
					return
				}
				if((!this.ruleForm.mima) && `yonghu` == this.tableName){
					this.$utils.msg(`密码不能为空`);
					return
				}
                if(`yonghu` == this.tableName && (this.ruleForm.mima!=this.ruleForm.mima2)){
                    this.$utils.msg(`两次密码输入不一致`);
                    return
                }
				if(`yonghu` == this.tableName && this.ruleForm.shouji&&(!this.$validate.isMobile(this.ruleForm.shouji))){
					this.$utils.msg(`手机应输入手机格式`);
					return
				}
				if(`yonghu` == this.tableName && this.ruleForm.youxiang&&(!this.$validate.isEmail(this.ruleForm.youxiang))){
					this.$utils.msg(`邮箱应输入邮件格式`);
					return
				}
				await this.$api.register(`${this.tableName}`, this.ruleForm, this.emailcode);
				this.$utils.msgBack('注册成功');;
			}
		}
	}
</script>

<style lang="scss" scoped>
	$color-primary: #4A4A4A;

	.content {
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		height: 100vh;
		box-sizing: border-box;
		background-image: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
	}
	
	.box {
		padding: 60rpx 40rpx;
		box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.1);
		margin: 0 5%;
		background-color: rgba(255, 255, 255, 0.95);
		border-radius: 24rpx;
		width: 90%;
		max-width: 600rpx;
	}
	
	.logo {
		padding: 0;
		margin: 0 0 60rpx;
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		
		image {
			width: 160rpx;
			height: 160rpx;
			border-radius: 50%;
			box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
		}
		
		.title {
			margin-top: 20rpx;
			font-size: 36rpx;
			font-weight: bold;
			color: $color-primary;
		}
	}
	
	.form-item {
		margin-bottom: 30rpx;
		
		.input {
			padding: 0 30rpx;
			background-color: #f8f8f8;
			color: #333;
			border-radius: 12rpx;
			width: 100%;
			height: 88rpx;
			font-size: 28rpx;
			box-sizing: border-box;
			border: 1px solid #eee;
		}
		
		.picker-view {
			padding: 0 30rpx;
			background-color: #f8f8f8;
			color: #333;
			border-radius: 12rpx;
			width: 100%;
			height: 88rpx;
			line-height: 88rpx;
			font-size: 28rpx;
			box-sizing: border-box;
			border: 1px solid #eee;
		}
	}
	
	.btn-submit {
		background-color: $color-primary;
		border-radius: 12rpx;
		color: #fff;
		width: 100%;
		font-size: 32rpx;
		height: 88rpx;
		line-height: 88rpx;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.2);
		transition: all 0.3s ease;
		
		&:active {
			transform: scale(0.98);
			opacity: 0.9;
		}
	}
	
	.links {
		margin-top: 40rpx;
		display: flex;
		justify-content: center;
		
		.link-item {
			padding: 20rpx 0;
			color: $color-primary;
			font-size: 26rpx;
		}
	}
</style>
